<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客诉id" prop="ksId">
        <el-input
          v-model="queryParams.ksId"
          placeholder="请输入客诉id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <!--      <el-form-item label="提交人" prop="issuerUser">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.issuerUser"-->
      <!--          placeholder="请输入提交人"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="审核人" prop="auditUser">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.auditUser"-->
      <!--          placeholder="请输入审核人"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="标题" prop="title">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.title"-->
      <!--          placeholder="请输入标题"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="发布时间" prop="issuerTime">-->
      <!--        <el-date-picker clearable-->
      <!--          v-model="queryParams.issuerTime"-->
      <!--          type="date"-->
      <!--          value-format="yyyy-MM-dd"-->
      <!--          placeholder="请选择发布时间">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="回复时间" prop="replyTime">-->
      <!--        <el-date-picker clearable-->
      <!--          v-model="queryParams.replyTime"-->
      <!--          type="date"-->
      <!--          value-format="yyyy-MM-dd"-->
      <!--          placeholder="请选择回复时间">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="0默认1同意2不同意3延后处理" prop="isAgree">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.isAgree"-->
      <!--          placeholder="请输入0默认1同意2不同意3延后处理"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--    <el-row :gutter="10" class="mb8">-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="primary"-->
    <!--          plain-->
    <!--          icon="el-icon-plus"-->
    <!--          size="mini"-->
    <!--          @click="handleAdd"-->
    <!--          v-hasPermi="['hmk:ksLetter:add']"-->
    <!--        >新增</el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="success"-->
    <!--          plain-->
    <!--          icon="el-icon-edit"-->
    <!--          size="mini"-->
    <!--          :disabled="single"-->
    <!--          @click="handleUpdate"-->
    <!--          v-hasPermi="['hmk:ksLetter:edit']"-->
    <!--        >修改</el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="danger"-->
    <!--          plain-->
    <!--          icon="el-icon-delete"-->
    <!--          size="mini"-->
    <!--          :disabled="multiple"-->
    <!--          @click="handleDelete"-->
    <!--          v-hasPermi="['hmk:ksLetter:remove']"-->
    <!--        >删除</el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="warning"-->
    <!--          plain-->
    <!--          icon="el-icon-download"-->
    <!--          size="mini"-->
    <!--          @click="handleExport"-->
    <!--          v-hasPermi="['hmk:ksLetter:export']"-->
    <!--        >导出</el-button>-->
    <!--      </el-col>-->
    <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    <!--    </el-row>-->

    <el-table v-loading="loading" :data="ksLetterList" @selection-change="handleSelectionChange" :row-style="rowStyle">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="id" align="center" prop="id"/>-->
      <el-table-column label="客诉id" align="center" prop="ksId"/>
      <!--      <el-table-column label="订单编码" align="center" prop="orderId" />-->
      <el-table-column label="提交人" align="center" prop="issuerUserName"/>
      <el-table-column label="审核人" align="center" prop="auditName"/>
      <el-table-column label="标题" align="center" prop="title"/>
      <!--      <el-table-column label="提交内容" align="center" prop="issuerContent" />-->
      <el-table-column label="发布时间" align="center" prop="issuerTime" width="180">
      </el-table-column>
      <!--      <el-table-column label="回复内容" align="center" prop="replyContent" />-->
      <el-table-column label="回复时间" align="center" prop="replyTime" width="180">
      </el-table-column>

      <el-table-column label="状态" align="center" prop="isAgree">
        <template #default="{row}">
          {{ isAgreeMap[row.isAgree] }}
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.isAgree == 3 && scope.row.auditUser == $store.state.user.id"
                     size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="showReplyDialog(scope.row)"
                     v-hasPermi="['hmk:ksLetter:edit']"
          >回复
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客诉工单站内信对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="客诉id" prop="ksId">
          <el-input v-model="form.ksId" placeholder="请输入客诉id"/>
        </el-form-item>
        <el-form-item label="订单编码" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单编码"/>
        </el-form-item>
        <el-form-item label="提交人" prop="issuerUser">
          <el-input v-model="form.issuerUser" placeholder="请输入提交人"/>
        </el-form-item>
        <el-form-item label="审核人" prop="auditUser">
          <el-input v-model="form.auditUser" placeholder="请输入审核人"/>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题"/>
        </el-form-item>
        <el-form-item label="提交内容">
          <editor v-model="form.issuerContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="发布时间" prop="issuerTime">
          <el-date-picker clearable
                          v-model="form.issuerTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择发布时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="回复内容">
          <editor v-model="form.replyContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="回复时间" prop="replyTime">
          <el-date-picker clearable
                          v-model="form.replyTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择回复时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="0默认1同意2不同意3延后处理" prop="isAgree">
          <el-input v-model="form.isAgree" placeholder="请输入0默认1同意2不同意3延后处理"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :visible.sync="visible"
      width="30%"
    >
      <template #title>
        <div class="custom-header">
          <span>{{ ksLetter.title + '方案审核' }}</span>
        </div>
      </template>
      <div style="margin-left: 10px;color: #8c939d">工单号: {{ ksLetter.ksId }}</div>
      <div v-if="ksLetter.orderId" style="margin-left: 10px;color: #8c939d">订单编码:
        {{ ksLetter.orderId.substring(0, ksLetter.orderId.length - 11) }}<span
          style="color: #409EFF;">{{ ksLetter.orderId.slice(-11) }}</span></div>
      <div style="margin-left: 10px;color: #8c939d">提交人: {{ ksLetter.issuerUserName }}</div>
      <div style="margin-left: 10px;color: #8c939d">提交时间: {{ ksLetter.issuerTime }}</div>
      <div style="margin-left: 10px;color: #8c939d;">审核内容: {{ ksLetter.issuerContent }}</div>
      <div style="margin-left: 10px;color: #8c939d;">客诉内容: {{ ksLetter.ksContent }}</div>
      <el-divider></el-divider>

      <el-form ref="ksForm" :model="ksForm" label-width="80px">

        <el-form-item label="是否同意">
          <el-switch v-model="ksForm.isAgree" active-text="同意" active-color="#13ce66" inactive-text="不同意"
                     active-value="1" inactive-value="2">
          </el-switch>
        </el-form-item>

        <el-form-item label="审批意见">
          <el-input type="textarea" v-model="ksForm.replyContent" placeholder="请在此输入审批意见"></el-input>
        </el-form-item>
        <div class="hjfBottomFoot">
          <el-button size="small" type="primary" @click="onSubmit()">提交</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  addKsLetter,
  delKsLetter,
  getKsLetter,
  getKsMessageNumber,
  listKsLetter,
  updateKsLetter
} from "@/api/hmk/ksLetter";

export default {
  name: "KsLetter",
  data() {
    return {
      visible: false,
      ksLetter: {
        id: null,
        title: null,
        issuerContent: null,
        issuerUserName: null,
        ksId: null,
        issuerTime: null,
        orderId: null,
        ksContent: null,
      },
      ksForm: {
        id: null,
        isAgree: 1,
        replyContent: null,
      },

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客诉工单站内信表格数据
      ksLetterList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ksId: null,
        orderId: null,
        issuerUser: null,
        auditUser: null,
        title: null,
        issuerContent: null,
        issuerTime: null,
        replyContent: null,
        replyTime: null,
        isAgree: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ksId: [
          {required: true, message: "客诉id不能为空", trigger: "blur"}
        ],
        orderId: [
          {required: true, message: "订单编码不能为空", trigger: "blur"}
        ],
        issuerUser: [
          {required: true, message: "提交人不能为空", trigger: "blur"}
        ],
        auditUser: [
          {required: true, message: "审核人不能为空", trigger: "blur"}
        ],
        title: [
          {required: true, message: "标题不能为空", trigger: "blur"}
        ],
        issuerContent: [
          {required: true, message: "提交内容不能为空", trigger: "blur"}
        ],
      }
    };
  },

  created() {
    this.getList();
  },
  computed: {
    isAgreeMap(data) {
      return {
        0: '默认',
        1: '同意',
        2: '不同意',
        3: '待处理',
      }
    }
  },
  methods: {
    rowStyle({row}) {
      if (row.auditUser == this.$store.state.user.id && row.isAgree == 3) {
        return {backgroundColor: 'rgba(255, 0, 0, 0.1)'};
      }
      return {};
    },
    async onSubmit() {
      if (!this.ksForm.replyContent) {
        this.$modal.msgWarning("审批意见必填!")
        return;
      }
      this.ksForm.id = this.ksLetter.id;
      await updateKsLetter(this.ksForm).then(res => {
        this.$modal.msgSuccess('已提交!');
        this.getList();
      })
      await getKsMessageNumber({isAgree: 3}).then(res => {
        if (res && res.data && res.data.ksMessageNumber) {
          this.$store.commit('SET_ksMessageNumber', res.data.ksMessageNumber);
        } else if (!res.data)
          this.$store.commit('SET_ksMessageNumber', null);
      })
      this.visible = false;
    },

    showReplyDialog(row) {
      this.ksLetter = row;
      this.visible = true;
    },
    /** 查询客诉工单站内信列表 */
    getList() {
      this.loading = true;
      listKsLetter(this.queryParams).then(response => {
        this.ksLetterList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        ksId: null,
        orderId: null,
        issuerUser: null,
        auditUser: null,
        title: null,
        issuerContent: null,
        issuerTime: null,
        replyContent: null,
        replyTime: null,
        isAgree: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客诉工单站内信";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getKsLetter(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客诉工单站内信";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateKsLetter(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addKsLetter(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除客诉工单站内信编号为"' + ids + '"的数据项？').then(function () {
        return delKsLetter(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hmk/ksLetter/export', {
        ...this.queryParams
      }, `ksLetter_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
.hjfBottomFoot {
  display: flex;
  justify-content: end;
}
</style>
